<template>
  <view>
    <!-- 搜索区域 -->
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>
    
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" class="carusole">
      <swiper-item v-for="(item,index) in swiperList" :key="index">
        <navigator class="swiper-item" :url="'/subpkg/goods-detail/goods-detail?goods_id='+ item.goods_id">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>
    
    <!-- 导航栏 -->
    <view class="home-nav">
      <view class="home-nav-item" v-for="(item, i) in navList" :key="i">
        <image :src="item.image_src" @click="navClickHandler(item)"></image>
      </view>
    </view>
  
    <!-- 楼层区 -->
    <view>
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层内容 -->
        <view class="floor-body">
            <navigator class="floor-body-left" :url="item.product_list[0].url">
              <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
            <view class="floor-body-right">
              <navigator :url="item2.url" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2 != 0">
                <image :src="item2.image_src" mode="widthFix" :style="{width: item2.image_width + 'rpx'}"></image>
              </navigator>
            </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import cartBadge from '@/mixins/cart-badge.js'
  export default {
    mixins: [cartBadge],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      // 获取轮播图数据
      async getSwiperList() {
        const {data: res} = await uni.$http.get('/api/public/v1/home/swiperdata')
        if(res.meta.status !== 200) return uni.showMsg('导航栏获取数据失败')
        this.swiperList = res.message
      },
      
      // 获取导航栏数据
      async getNavList() {
        const {data: res} = await uni.$http.get('/api/public/v1/home/catitems')
        if(res.meta.status !== 200) return uni.showMsg('导航栏数据获取失败')
        this.navList = res.message
      },
      
      // 获取楼层列表
      async getFloorList() {
        const {data: res} = await uni.$http.get('/api/public/v1/home/floordata')
        if(res.meta.status !== 200) return uni.showMsg('获取楼层列表数据失败！')
        this.floorList = res.message
        res.message.forEach(item => {
          item.product_list.forEach(prop => {
            prop.url = '/subpkg/goods-list/goods-list?' + prop.navigator_url.split('?')[1]
          })
        })
      },
      
      // 点击分类跳转页面
      navClickHandler(item) {
        if(item.name = '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
    
      // 去搜索页面
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      },
    
    }
  }
</script>

<style lang="scss">

.carusole {
  height: 330rpx;
  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}
.home-nav {
  display: flex;
  justify-content: space-around;
  margin: 15rpx 0;
  .home-nav-item,
  image {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  width: 100%;
  height: 60rpx;
}

.floor-body {
  display: flex;
  padding-left: 10rpx;
}

.floor-body-right {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.search-box {
  position: sticky;
  top: 0;
  z-index: 666;
}
</style>
